const express = require('express'),
      membersService = require('../services/membersService');

const membersRouter = express.Router();

membersRouter
    .route('/')

    .post(async (req, res, next) => {
        try {
            const memberData = req.body;

            const rows = await membersService.create(
                memberData.surname,
                memberData.name,
                memberData.lastName,
                memberData.dateOfBirth
            );

            res.json(rows[0]);
        } catch(err) {
            next(err);
        }
    })

    .get(async (req, res, next) => {
        try {
            const rows = await membersService.getAll();

            res.json(rows);
        } catch(err) {
            next(err);
        }
    })

    .delete(async (req, res, next) => {
        try {
            const { memberId } = req.body,
                  rows = await membersService.delete(memberId);

            res.json(rows[0]);
        } catch(err) {
            next(err);
        }
    });

module.exports = membersRouter;
